//
//  ServiceViewController.m
//  UESTCPioneer
//
//  Created by sss on 14-9-24.
//  Copyright (c) 2014年 Sway. All rights reserved.
//

#import "ServiceViewController.h"
#import "PartyDataGuideEntity.h"



#import "UPTitleCell.h"
#import "UPMainInfoCell.h"

#import "NewsDetailViewController.h"

#pragma mark - 颜色
#define kNavigationBarColor [UIColor colorWithRed:192.0/255.0 green:57.0/255.0 blue:43.0/255.0 alpha:1]
#define KSPEColor [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1]
#define KBtnTextColor [UIColor colorWithRed:119.0/255.0 green:119.0/255.0 blue:119.0/255.0 alpha:1]

@interface ServiceViewController ()<UITableViewDataSource,UITableViewDelegate,UPMainInfoCellDelegate>{
    UIButton *lawGuideBtn;
    UIButton *freshmenGuideBtn;
    UIView *slideBar;
    NSMutableArray *resultArr;
    
    NSArray *lawDataArr;
    NSArray *freshmenDataArr;
    
    UITableView *table;
    BOOL isFreshmen;
    UIActivityIndicatorView *actView;
}

@end

@implementation ServiceViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(id)init{
    self = [super init];
    if (self) {
        self.btnTitleNameArr = @[@"办事指南",@"新生指南"];
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    isFreshmen = NO;
    resultArr = [[NSMutableArray alloc]init];
    lawDataArr = [[NSArray alloc]init];
    freshmenDataArr = [[NSArray alloc] init];
    [self initTable];
    [self.view1 addSubview:table];
    [self getLawGuideList];
    [self getFreshmenGuideList];
    [self setActView];
    NSLog(@"--------------------test for s------------------");
    NSLog(@"the head veiw subs selff view:%@",self.headView.subviews);
    NSLog(@"--------------------test for s------------------");
    
}

#pragma mark -
#pragma mark 初始化

-(void)setActView{
    CGFloat x = (self.view.frame.size.width - 37)/2;
    CGFloat y = ([UIApplication sharedApplication].keyWindow.bounds.size.height - 37)/2;
    UIView *indicationView = [[UIView alloc] initWithFrame:CGRectMake(x, y, 38, 38)];
   
    actView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    actView.backgroundColor = [UIColor grayColor];
    [indicationView addSubview:actView];
    
    [[UIApplication sharedApplication].keyWindow addSubview:indicationView];
    [actView startAnimating];
}


#pragma mark -
#pragma mark 设置table
-(void)initTable{
    table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view1.frame.size.height) style:UITableViewStyleGrouped];
    table.delegate = self;
    table.dataSource = self;
    
    [table registerClass:[UPTitleCell class] forCellReuseIdentifier:@"title"];
    [table registerClass:[UPMainInfoCell class] forCellReuseIdentifier:@"lawContent"];
    [table registerClass:[UPMainInfoCell class] forCellReuseIdentifier:@"freshmenContent"];
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    [table setTableFooterView:view];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.leveyTabBarController.navigationItem.title = @"党员服务";
    
    UIButton *leftMenu = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftMenu setImage:[UIImage imageNamed:@"menu.png"] forState:UIControlStateNormal];
    [leftMenu setImageEdgeInsets:UIEdgeInsetsMake(12, 12, 12, 42)];
    [leftMenu addTarget:self action:@selector(leftMenuClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *button  = [helper BarButtonItemWithUIButton:leftMenu ButtonOrigin:CGPointMake(0, 0)];
    [self.leveyTabBarController.navigationItem setLeftBarButtonItem:button];
    
    [self.leveyTabBarController.navigationItem setRightBarButtonItem:nil];
}
-(void)leftMenuClick{
    [self.leveyTabBarController.revealSideViewController pushOldViewControllerOnDirection:PPRevealSideDirectionLeft animated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


#pragma mark -
#pragma mark tabel delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return resultArr.count;
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 55;
    }
    else if (indexPath.row == 1){
        
        CGFloat height;
        if (!isFreshmen) {
            PartyDataGuideEntity *entity =resultArr[indexPath.section];
            NSString *picStr= entity.picUrl;
            
            if ([picStr isEqualToString:@""]||picStr == nil){
                height=110;
            }
            else {
                height=110+imageViewWidth + 100;
            }
        }else{
            height = 110;
        }
        
        return height;
    }
    else
        return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0) {
        UPTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"title" forIndexPath:indexPath];
        if (!isFreshmen) {
            PartyDataGuideEntity *entity = resultArr[indexPath.section];
            [cell setTitle:entity.title];
        }else{
            NSDictionary *dic = resultArr[indexPath.section];
            [cell setTitle:[dic objectForKey:@"name"]];
            
        }

        return cell;
    }else{
        
        if (!isFreshmen) {
            UPMainInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"lawContent" forIndexPath:indexPath];
            PartyDataGuideEntity *entity = resultArr[indexPath.section];
            NSString *picUrlStr = [baseUrl stringByAppendingString:entity.picUrl];

            [cell setImageUrlArray:@[picUrlStr]];
            [cell setNewsBody:entity.content];
            cell.theId = indexPath.section;
            cell.delegate = self;
            return cell;
        }else{
            UPMainInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"freshmenContent" forIndexPath:indexPath];
            NSDictionary *dic = resultArr[indexPath.section];
            [cell setNewsBody:[dic objectForKey:@"content"]];
            cell.theId = indexPath.section;
            cell.delegate = self;
            return cell;
        }
        
    }
}


#pragma mark -
#pragma mark get data

-(void)getFreshmenGuideList{
    [actView startAnimating];
        NSDictionary *requestDic = @{@"type": @"getFreshman",
                                     @"page":@"1"};
        [NetworkCenter AFRequestWithData:requestDic SuccessBlock:^(AFHTTPRequestOperation *operation, id resultObject) {
            NSLog(@"success");
            NSArray *Arr = [NSJSONSerialization JSONObjectWithData:resultObject options:NSJSONReadingMutableLeaves error:nil];
            freshmenDataArr = Arr;
        } FailureBlock:^(AFHTTPRequestOperation *operation, NSError *error) {
            [actView stopAnimating];
            [Alert showAlert:@"网络连接失败"];
        }];
}

-(void)getLawGuideList{
    [actView startAnimating];
    NSDictionary *requestData = @{@"type":@"getZhinanList",@"page":@"1"};
    NSString *entityName = @"PartyDataGuideEntity";
    [NetworkCenter RKRequestWithData:requestData EntityName:entityName Mapping:[Mapping PartyDataGuideEntityMapping] SuccessBlock:^(NSArray *resultArray) {
        NSLog(@"sucess");
        lawDataArr = resultArray;
        resultArr = [NSMutableArray arrayWithArray:lawDataArr];
        [table reloadData];
        [actView stopAnimating];
    } failure:^(NSError *error) {
        [actView stopAnimating];
        [Alert showAlert:@"网络连接失败"];
    }];

}

#pragma mark -
#pragma mark 重写父类点击方法
-(void)btnClickAction:(UIButton *)btn{
    NSLog(@"--------------------test for s------------------");
    NSLog(@"the head veiw subs:%@",btn.superview.subviews);
    NSLog(@"--------------------test for s------------------");
    [resultArr removeAllObjects];
    [table removeFromSuperview];

    if (self.isView1Display) {
        [self.view2 addSubview:table];
    }else{
        [self.view1 addSubview:table];
    }
    
    if (btn.tag == 1) {
        isFreshmen = NO;
        resultArr = [NSMutableArray arrayWithArray:lawDataArr];
    }else{
        isFreshmen = YES;
        resultArr = [NSMutableArray arrayWithArray:freshmenDataArr];
    }

    [table reloadData];
}


#pragma mark -
#pragma mark main cell delegate
-(void)WholeNewsButtonClick:(NSInteger)theId{
    if (isFreshmen) {
        NSDictionary *dic = freshmenDataArr[theId];
        NSLog(@"dic:%@",dic);
        NewsDetailViewController *detailVC = [[NewsDetailViewController alloc]initWithImgUrlArr:nil title:[dic objectForKey:@"name"] content:[dic objectForKey:@"content"]];
        [self.leveyTabBarController.navigationController pushViewController:detailVC animated:YES];
    }
    else{
        PartyDataGuideEntity *entity = lawDataArr[theId];
        NSString *picURLStr = entity.picUrl;
        NSMutableArray *arr = [NSMutableArray arrayWithObject:picURLStr];
        if ([picURLStr isEqualToString:@""]) {
            [arr removeAllObjects];
        }
        NewsDetailViewController *detailVC = [[NewsDetailViewController alloc] initWithImgUrlArr:arr title:entity.title content:entity.content];
        [self.leveyTabBarController.navigationController pushViewController:detailVC animated:YES];
    }
}


@end
